class Storage {

  get(key: string) {
    const value = localStorage.getItem(key)
    try {
      return JSON.parse(value!)
    } catch (error) {
      return value
    }
  }

  set(key: string, value: object | string) {
    if (typeof value === 'object') {
      value = JSON.stringify(value)
    }
    localStorage.setItem(key, value)
  }

  remove(key: string) {
    localStorage.removeItem(key)
  }
}

/* 单例懒加载 */
const getStorage = (() => {
  let storage: Storage
  return () => {
    if (!storage) {
      return storage = new Storage()
    } else {
      return storage
    }
  }
})()


/* token */
const HUIKE_TOKEN = 'HUIKE_TOKEN'
export const setToken = (token: string) => getStorage().set(HUIKE_TOKEN, token)
export const getToken = () => getStorage().get(HUIKE_TOKEN)
export const removeToken = () => getStorage().remove(HUIKE_TOKEN)

/* subject dict */
const HUIKE_SUBJECTS = 'HUIKE_SUBJECTS'
export const setSubjectDict = (subjects: any[]) => getStorage().set(HUIKE_SUBJECTS, subjects)
export const getSubjectDict = () => getStorage().get(HUIKE_SUBJECTS)

/* cityList */
const HUIKE_CITYS = 'HUIKE_CITYS'
export const getCitys = () => getStorage().get(HUIKE_CITYS)
export const setCitys = (citys: any[]) => getStorage().set(HUIKE_CITYS, citys)